package gw.com.android.presenter;

import android.os.Bundle;

import com.gwtsz.android.rxbus.RxBus;

import org.json.JSONException;
import org.json.JSONObject;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.terminal.GTSDataListener;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import www.com.library.app.Logger;

/**
 * Created by jay on 2017/7/5.
 */

public class PhoneLoginPresenter extends BasePresenter{

    private final String TAG = PhoneLoginPresenter.class.getSimpleName();
    private static PhoneLoginPresenter mPhoneLoginPresenter  = null;
    private OnMobileReqResultListener mPhonecallback;
    public boolean isLoading = false;
    private int mPhoneLoginSeq = 0;

    public PhoneLoginPresenter() {
        init();
        rxBusRegister = RxBus.getInstance().register(GTSConst.REPLY_TYPE_BINDCUSTOMER, Bundle.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>(){

                    @Override
                    public void accept(@NonNull Bundle bundle) throws Exception {
                        onResultPresenter(bundle);
                    }
                });
    }
    public static synchronized PhoneLoginPresenter instance() {
        if (mPhoneLoginPresenter == null){
            mPhoneLoginPresenter = new PhoneLoginPresenter();
        }
        return mPhoneLoginPresenter;
    }

    /**
     *
     * 获取手机号绑定的账号列表
     * @param loginType
     *            接口类型
     */
    public void PhoneLoginFun(String loginType,int mSeq,String loginName,String loginPassWord
                                                    ,OnMobileReqResultListener mCallback){
        if(isLoading){
            return;
        }
        mPhoneLoginSeq = mSeq;
        isLoading = true;
        this.mPhonecallback = mCallback;
        JSONObject info = new JSONObject();
        try {
            info.put("mobileNo",loginName);
            info.put("password",loginPassWord);
            info.put("mobilePrefix","86");
            GTSDataListener.instance().addSeqList(mPhoneLoginSeq+"", AppContances.NORMALREQ_TYPE_GET_CUSTOMERBYMOBILE);
            AppTerminal.instance().NormalReqConfigSvr(ConfigType.HTTP_GET_GET_CUSTOMERBYMOBILE_TAG, info.toString(), mSeq);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * 绑定账号的接口
     * @param loginType
     *            接口类型
     */
    public void BindCustomerFun(String loginType,int mSeq,int accountType,String loginName,String loginPassWord
                                                                        ,OnMobileReqResultListener mCallback){
        if(isLoading){
            return;
        }
        isLoading = true;
        mPhoneLoginSeq = mSeq;
        this.mPhonecallback = mCallback;
        JSONObject info = new JSONObject();
        try {
            info.put("customerNumber",loginName);
            if(accountType == GTConfig.ACC_TYPE_DEMO){
                info.put("tradeEnvironment","DEMO");
            }else{
                info.put("tradeEnvironment","REAL");
            }
            info.put("password",loginPassWord);
            GTSDataListener.instance().addSeqList(mPhoneLoginSeq+"", AppContances.NORMALREQ_TYPE_BINDCUSTOMER);
            AppTerminal.instance().NormalReqConfigSvr(ConfigType.HTTP_GET_BINDCUSTOMER_TAG, info.toString(), mSeq);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    /**
     * Created by Jay on 2017/7/5.
     * 手机登录请求账号信息回调
     */
    public interface OnMobileReqResultListener {
        /**
         * 按钮点击监听器回调函数
         *
         * @param mObject 接口返回结果
         */
        void reqResultSuccess(Object mObject);

        void reqResultFail(int failCode);
    }

    @Override
    protected void onResultPresenter(Bundle bundle) {
        isLoading = false;
        if (null != bundle) {
            int iValue = bundle.getInt("iValue");
            String rgsUrl = bundle.getString("strObject");
            Logger.i(TAG + "rgsUrl：：" + rgsUrl + "..iValue::" + iValue);
            if (iValue == 0 && mPhoneLoginSeq == bundle.getInt("iNotification")) {
                try {
                    JSONObject jsonRgsUrl = new JSONObject(rgsUrl);
                    JSONObject jsonResult = jsonRgsUrl.getJSONObject("result");
                    String resultCode = jsonResult.getString("code");
                    if ("OK".equals(resultCode) && jsonResult.has("result")) {
                        if(!jsonResult.isNull("error")){
                            int failCode ;
                            if(!jsonResult.isNull("error")){
                                JSONObject resultObj = jsonResult.getJSONObject("error");
                                String errorCode = resultObj.getString("code");
                                if("RA0001".equals(errorCode)){
                                    failCode = 1;
                                }else if("RA0002".equals(errorCode)){
                                    failCode = 36;
                                }else if("RA0007".equals(errorCode)){
                                    failCode = 7;
                                }else{
                                    failCode =0;
                                }
                            }else{
                                failCode =0;
                            }
                            onResultFail(failCode);
                        }else{
                            mPhonecallback.reqResultSuccess(jsonResult);
                        }

                    } else {
                        int failCode ;
                        if(!jsonResult.isNull("error")){
                            JSONObject resultObj = jsonResult.getJSONObject("error");
                            String errorCode = resultObj.getString("code");
                            if("RA0001".equals(errorCode)){
                                failCode = 1;
                            }else if("RA0002".equals(errorCode)){
                                failCode = 36;
                            }else if("RA0007".equals(errorCode)){
                                failCode = 7;
                            }else{
                                failCode =0;
                            }
                        }else{
                            failCode =0;
                        }
                        onResultFail(failCode);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    onResultFail(0);
                }
            }else{
                if(iValue == 1012){
                    onResultFail(1012);
                }else{
                    onResultFail(0);
                }
            }

        }
    }

    private void onResultFail(int failCode){
        if(null != mPhonecallback){
            mPhonecallback.reqResultFail(failCode);
        }
    }
}
